'''
Descripttion: 
version: V1.0
Author: zyx
Date: 2025-03-10 08:54:43
LastEditors: zyx
LastEditTime: 2025-03-26 11:19:20
'''
'''
Descripttion: 
version: V1.0
Author: zyx
Date: 2025-03-10 08:54:43
LastEditors: zyx
LastEditTime: 2025-03-24 10:38:04
'''
'''
Descripttion: 
version: V1.0
Author: zyx
Date: 2025-01-17 10:18:25
LastEditors: zyx
LastEditTime: 2025-03-04 11:17:39
'''
import re
import os
from compass_metrics.security_metric.utils import save_json
from compass_metrics.security_metric.utils import TMP_PATH,JSON_REPOPATH
from compass_metrics.document_metric import Industry_Support
import json
REPOPATH = TMP_PATH

def vulnerablity_feedback_channels(url,version):
    '''Check if the specified folder contains documents with Chinese characters'''
    repo_name = os.path.basename(url) + "-" + version
    
    # if repo_name not in os.listdir(REPOPATH):
    #     print(f"Cloning {repo_name} repository...")
    #     clone_repo(url)
        
    json_path = os.path.join(JSON_REPOPATH, f"{repo_name}.json")

    if f"{repo_name}.json" not in os.listdir(JSON_REPOPATH):
        print(f"Generating json file for {repo_name}...")
        a = Industry_Support("",[url],version)

        # return ValueError(f"Start by performing the document quantity metric...")

    vulnerablity_feedback = vulnerablity_feedback_to_all_documents(json_path)
    return vulnerablity_feedback

def vulnerablity_feedback_to_all_documents(json_path):
    '''Check if the specified folder contains documents with Chinese characters'''
    ans = {"vulnerablity_feedback_channels":0, "vulnerablity_feedback_channels_details":[]}
    with open(json_path, 'r', encoding='utf-8') as f:
        json_data = json.load(f)
    for document_details in json_data["folder_document_details"]:
        name = document_details["name"]
        path = document_details["path"]
        with open(os.path.join(REPOPATH, path), 'r', encoding='utf-8', errors='ignore') as f:
            content = f.read()
        feedback = has_vulnerablity_feedback(content)
        if feedback['has_vulnerablity_feedback']:
            ans["vulnerablity_feedback_channels"] += 1
            ans["vulnerablity_feedback_channels_details"].append({'document_name': name, 'document_path': path, 'vulnerablity_feedback': feedback['contact_info']})
            # ans.append({'document_name': name, 'document_path': path, 'vulnerablity_feedback': feedback['contact_info']})
    return ans

def has_vulnerablity_feedback(content):
    '''Extract the vulnerability feedback information from the document content along with their positions'''
    feedback_patterns = [
        r'漏洞反馈',  # Chinese pattern for vulnerability feedback
        r'vulnerability feedback',  # English pattern for vulnerability feedback
        r'vulnerabilities', 
        r'security feedback',
        r'security contact',
        r'security issues',
        r'security vulnerabilities',
        r'security advisories',
        r'security reports',
        r'security concerns',
        r'security bugs',
        r'security patches',
        r'security',
        r'漏洞',
        r'安全反馈',
        r'安全问题',
        r'安全漏洞',
        r'安全报告',
        r'安全建议',
        r'安全补丁',
        r'安全',
        r'patches',
        r'补丁',
    ]
    
    feedback_info = []
    lines = content.split('\n\n')
    for line in lines:
        for pattern in feedback_patterns:
            matches = re.search(pattern, line, re.IGNORECASE)
            if matches and line not in feedback_info:
                feedback_info.append(line)
    ans = {
        'has_vulnerablity_feedback': 1 if feedback_info else 0,
        'contact_info': feedback_info
    }
    
    return ans

if __name__ == "__main__":
    with open(r'C:\Users\zyx\Desktop\文档数量\tmp\git-lfs\README.md', 'r', encoding='utf-8', errors='ignore') as f:
        content = f.read()
    # print(extract_vulnerablity_feedback_with_position(content))
    # save_json(vulnerablity_feedback_channels('https://github.com/numpy/numpy'),"漏洞反馈信息.json")
    save_json(vulnerablity_feedback_channels('https://github.com/git-lfs/git-lfs'),"漏洞反馈信息.json")
    # with open(r"C:\Users\zyx\Desktop\文档数量\tmp\numpy\README.md", 'r', encoding='utf-8') as f:
    #     content = f.read()
    # print(has_vulnerablity_feedback(content))
    # print(has_vulnerablity_feedback(r"C:\Users\zyx\Desktop\文档数量\tmp\numpy\README.md"))
    # parten = r'patches'
    # content = 'See [CONTRIBUTING.md](CONTRIBUTING.md) for info on working on Git LFS and sending patches. Related projects are listed on the [Implementations wiki page](https://github.com/git-lfs/git-lfs/wiki/Implementations).'
    # matches = re.finditer(parten, content, re.IGNORECASE)
    # for match in matches:
    #     print(match.group())
    # print(re.search(parten, content, re.IGNORECASE))
    # if re.search(parten, content, re.IGNORECASE):
    #     print('True')
    # print(extract_vulnerablity_feedback_with_position(conte

